package cn.wk.pet.controller;

import cn.wk.basic.utils.AjaxResult;
import cn.wk.basic.utils.PageBean;
import cn.wk.basic.utils.SystemContext;
import cn.wk.org.domain.Shop;
import cn.wk.org.service.IShopService;
import cn.wk.pet.domain.SearchMasterMsg;
import cn.wk.pet.query.SearchMasterMsgQuery;
import cn.wk.pet.service.ISearchMasterMsgService;
import cn.wk.user.domain.LoginInfo;
import cn.wk.user.domain.User;
import cn.wk.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 发布寻主信息
 */
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private IShopService shopService;
    /**
     * 添加寻主信息
     */
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/publish")
    public AjaxResult searchMasterMsg(@RequestBody SearchMasterMsg searchMasterMsg,HttpServletRequest request){
       try {
           //获取当前登录对象
           LoginInfo loginInfo = SystemContext.getLoginInfo(request);
           //通过logininfo的id去获取user
           User user = userService.findByLogininfoId(loginInfo.getId());
           //把user设置到searchMasterMsg中
           searchMasterMsg.setUser(user);
           //保存数据
           searchMasterMsgService.publish(searchMasterMsg);
           return new AjaxResult();
       }catch (Exception e){
           e.printStackTrace();
           return new AjaxResult(e.getMessage());
       }

    }

    /**
     * 查询待处理寻主信息
     * @param searchMasterMsgQuery
     * @return
     */
    @PostMapping("/pendingMessage")
    public PageBean<SearchMasterMsg> pendingMessage(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request){
        try {
            //获取当前登录对象
            LoginInfo loginInfo = SystemContext.getLoginInfo(request);
            //通过当前登录对象去查询emp再去查询shop
            Shop shop = shopService.findByLoginInfoId(loginInfo.getId());
            //把shopid添加到query中去
            searchMasterMsgQuery.setShopId(shop.getId());
            return searchMasterMsgService.queryAll(searchMasterMsgQuery);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
